package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.HashSet;
import java.util.Set;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-21 09:18
 */
public class BT824山羊拉丁文 {


    public static void main(String[] args) {

        System.out.println(toGoatLati2("Each word consists of lowercase and uppercase letters only"));

    }

    public static String toGoatLatin(String sentence) {
        String[] array = sentence.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder sbA = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            String ss = array[i];

            String ssNew = "";
            if (ss.charAt(0) == 'a' || ss.charAt(0) == 'e' || ss.charAt(0) == 'i' || ss.charAt(0) == 'o' || ss.charAt(0) == 'u'
                    || ss.charAt(0) == 'A' || ss.charAt(0) == 'E' || ss.charAt(0) == 'I' || ss.charAt(0) == 'O' || ss.charAt(0) == 'U') {
                ssNew = ss + "ma";
            } else {
                ssNew = ss.substring(1) + ss.charAt(0) + "ma";
            }
            stringBuilder.append(ssNew);
            sbA.append("a");
            stringBuilder.append(sbA);
            if (i != array.length - 1) {
                stringBuilder.append(" ");
            }
        }
        return stringBuilder.toString();
    }

    public static String toGoatLati2(String sentence) {
        Set<Character> vowels = new HashSet<Character>() {{
            add('a');
            add('e');
            add('i');
            add('o');
            add('u');
            add('A');
            add('E');
            add('I');
            add('O');
            add('U');
        }};

        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder sbA = new StringBuilder();
        int j = 0;
        for (int i = 0; i < sentence.length(); i++) {
            if (sentence.charAt(i) != ' ' || i == sentence.length() - 1) {
                if (i != sentence.length() - 1) {
                    continue;
                }
            }

            if (vowels.contains(sentence.charAt(j))) {
                if (i == sentence.length() - 1) {
                    stringBuilder.append(sentence, j, i +1);
                } else {
                    stringBuilder.append(sentence, j, i);
                }

            } else {
                if (i == sentence.length() - 1) {
                    stringBuilder.append(sentence, j + 1, i + 1);
                } else {
                    stringBuilder.append(sentence, j + 1, i);
                }
                stringBuilder.append(sentence.charAt(j));
            }
            stringBuilder.append("ma");
            sbA.append("a");
            stringBuilder.append(sbA);
            if (i != sentence.length() - 1) {
                stringBuilder.append(" ");
            }
            j = i + 1;
        }
        return stringBuilder.toString();
    }
}
